package com.yitiao.socket.demo;

import java.io.*;
import java.net.*;

public class Client {
    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket();
//        socket.setSoTimeout(3000);
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(),2000));

        System.out.println("connecting success.");
        System.out.println("client info:["+socket.getLocalAddress()+":"+socket.getLocalPort()+"]");
        System.out.println("server info:["+socket.getInetAddress()+":"+socket.getPort()+"]");

        // 发送消息，从键盘输入
        InputStream in = System.in;
        BufferedReader input = new BufferedReader(new InputStreamReader(in));

        // 得到socket输出流，并转成打印流
        OutputStream outputStream = socket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);

        InputStream inputStream = socket.getInputStream();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        boolean falg = true;
        // 将从键盘输入流得到的数据放到输出的打印流中，发送出去。
        do {
            String line = input.readLine();
            if (line.equals("bye")){
                falg =false;
            }
            printStream.println(line);

            final String readLine = reader.readLine();
            System.out.println(readLine);
        }while (falg);


        // 资源释放
        inputStream.close();
        reader.close();

        socket.close();
    }
}
